home *** CD-ROM | disk | FTP | other *** search
/ Aminet 38 / Aminet 38 (2000)(Schatztruhe)[!][Aug 2000].iso / Aminet / misc / math / libalgo.lha / algomath / src / primeab.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-30  |  1.3 KB  |  132 lines

  1. /* prime number befor x<0/after x>0 a given number t 
  2. negative t return a negative result */
  3.  
  4. #include "defs.h"
  5.  
  6. int am_prime_ba(int t, int x)
  7. {
  8.  
  9. int a,m=0,k=0;
  10. int s[2];
  11.  
  12. if(t<0){
  13.     t=-t;
  14.     k=1;
  15.     }
  16.     
  17. if(x<0){
  18.     if(t>5){
  19.     a=t%6;
  20.     if(a<2){
  21.         s[0]=4;
  22.         s[1]=2;
  23.         m=t-a-1;
  24.         }
  25.     else{
  26.         s[0]=2;
  27.         s[1]=4;
  28.         m=t-a+1;
  29.         }
  30.  
  31.     for(;;){
  32.         if(am_isPrime(m)){
  33.             if(k)
  34.                 return -m;
  35.             else
  36.                 return m;
  37.             }
  38.         m=m-s[0];
  39.         if(am_isPrime(m)){
  40.             if(k)
  41.                 return -m;
  42.             else
  43.                 return m;
  44.             }
  45.         m=m-s[1];
  46.     }
  47.     }
  48.     else if(t==3){
  49.             if(k)
  50.                 return -2;
  51.             else
  52.                 return 2;
  53.             }
  54.     else if(t>3){
  55.             if(k)
  56.                 return -3;
  57.             else
  58.                 return 3;
  59.             }
  60.     else if(t<3)
  61.         return 0;
  62. }
  63. else if(x>0){
  64.     if(t>5){
  65.     a=t%6;
  66.     if(a==0){
  67.         s[0]=4;
  68.         s[1]=2;
  69.         m=t+1;    
  70.         }
  71.     else if(a<5){
  72.         a=6-a;
  73.         s[0]=2;
  74.         s[1]=4;
  75.         m=t+a-1;
  76.         }
  77.     else if(a==5){
  78.             s[0]=4;
  79.             s[1]=2;
  80.             m=t+2;
  81.     }
  82.     for(;;){
  83.         if(am_isPrime(m)){
  84.             if(k)
  85.                 return -m;
  86.             else
  87.                 return m;
  88.             }
  89.         m=m+s[0];
  90.         if(am_isPrime(m)){
  91.             if(k)
  92.                 return -m;
  93.             else
  94.                 return m;
  95.             }
  96.         m=m+s[1];
  97.     }
  98.     }
  99.     else if(t<2){
  100.             if(k)
  101.                 return -2;
  102.             else
  103.                 return 2;
  104.             }
  105.     else if(t==2){
  106.             if(k)
  107.                 return -3;
  108.             else
  109.                 return 3;
  110.             }
  111.     else if(t<5){
  112.             if(k)
  113.                 return -5;
  114.             else
  115.                 return 5;
  116.             }
  117.     else{
  118.             if(k)
  119.                 return -7;
  120.             else
  121.                 return 7;
  122.             }
  123. }
  124. else if(x==0){
  125.             if(k)
  126.                 return -t;
  127.             else
  128.                 return t;
  129.         }
  130.  
  131. return 0;
  132. }